package com.gs.synedocket.dialog;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;


public class ToastDialogUtils {
    private ToastDialog dialog;
    private Context context;
    private static volatile ToastDialogUtils instance;
    private int duration = 2000;

    private ToastDialogUtils() {
    }

    public static ToastDialogUtils getInstance() {
        if (instance == null) {
            synchronized (ToastDialogUtils.class) {
                if (instance == null) {
                    instance = new ToastDialogUtils();
                }
            }
        }
        return instance;
    }

    public void showInfoMessage(Context context, String msg, int duration) {
        dismiss();
        this.duration = duration;
        setDialog(context, msg, true);
        if (dialog != null) {
            dialog.show();
            dismissAfter2s();
        }
    }

    public void showInfoMessage(Context context, String msg) {
        showInfoMessage(context, msg, duration);
    }

    public void showInfoMessage(Context context, int resId) {
        showInfoMessage(context, context.getString(resId));
    }

    public void showInfoMessage(Context context, int resId, int duration) {
        this.duration = duration;
        showInfoMessage(context, context.getString(resId));
    }


    private void setDialog(Context ctx, String msg, boolean cancelable, int resId) {
        context = ctx;
        if (!isContextValid()) {
            return;
        }
        dialog = new ToastDialog(ctx).setMessage(msg).setIco(resId);
        dialog.setMessage(msg);
        dialog.setIco(resId);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(cancelable);        // back键是否可dismiss对话框

    }

    private void setDialog(Context ctx, String msg, boolean cancelable) {
        setDialog(ctx, msg, cancelable, 0);
    }

    /**
     * 关闭对话框
     */
    public void dismiss() {
        if (isContextValid() && dialog != null && dialog.isShowing())
            dialog.dismiss();
        dialog = null;
    }

    /**
     * 计时关闭对话框
     */
    private void dismissAfter2s() {
        new Thread(() -> {
            try {
                Thread.sleep(duration);
                handler.sendEmptyMessage(0);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

    private Handler handler = new Handler(Looper.getMainLooper()) {
        public void handleMessage(Message msg) {
            if (msg.what == 0)
                dismiss();
        }
    };

    /**
     * 判断parent view是否还存在
     * 若不存在不能调用dismiss，或setDialog等方法
     *
     * @return
     */
    private boolean isContextValid() {
        if (context == null)
            return false;
        if (context instanceof Activity) {
            Activity act = (Activity) context;
            return !act.isFinishing();
        }
        return true;
    }


}
